<?php

class Application_Model_Mapper_TurnberichtMapper
{
	protected $_dbTable;

	public function setDbTable ($dbTable)
	{
		
		if (is_string($dbTable))
		{
			$dbTable = new $dbTable();
		}
		if (! $dbTable instanceof Zend_Db_Table_Abstract)
		{
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
		
	}

	public function getDbTable ()
	{
		
		if (null === $this->_dbTable)
		{
			$this->setDbTable('Application_Model_DbTable_Turnbericht');
		}
		return $this->_dbTable;
		
	}

	public function add (Application_Model_Turnbericht $turnbericht)
	{
		$generatedid = Helper_Idgenerator::getid();
		$generatedidcheck = $this->fetchID($generatedid);
		if(empty($generatedidcheck)){
		
			$data = array(
					'id' => $generatedid,
					'turn' => $turnbericht->getTurn(),
					'created' => date('Y-m-d H:i:s'),
					'erstellerid' => $turnbericht->getErstellerid(),
					'titel' => "",
					'inhalt' => $turnbericht->getInhalt(),
					'papierkorb' => 0,
					'tag' => $turnbericht->getTag(),
					'distanz' => $turnbericht->getDistanz(),
					'starthafen' => $turnbericht->getStarthafen(),
					'zielhafen' => $turnbericht->getZielhafen()
			);
	
			return $this->getDbTable()->insert($data);
		}
		else{
			$this->add($turnbericht);
		}
		
	}
	
	
	public function delete($id)
	{
		$table = $this->getDbTable();
		$where = $table->getAdapter()->quoteInto('id = ?', $id);
		$table->delete($where);
	}


	public function fetchID($id)
	{
		$db = Zend_Db_Table::getDefaultAdapter();
		$entry = new Zend_Db_Select($db);
		$entry->from('turnbericht');
		$entry->where('turnbericht.id = ?', $id);
		return $entry->query()->fetch();
	}


	public function fetchTurn($id)
	{
		$db = Zend_Db_Table::getDefaultAdapter();
		$entry = new Zend_Db_Select($db);
		$entry->from('turnbericht', array('id As idturn', '*'));
		$entry->where('turnbericht.turn = ?', $id);
		$entry->where('turnbericht.papierkorb = 0');
		
		$entry->joinleft(
				array('hafenstart' => 'hafen'),
				'turnbericht.starthafen = hafenstart.id',
				array('name As Starthafenname', 'id As Startid'));

		$entry->joinleft(
				array('hafenziel' => 'hafen'),
				'turnbericht.zielhafen = hafenziel.id',
				array('name As Zielhafenname', 'id As Zielid'));
		$entry->order('tag ASC');
		return $entry->query()->fetchall();
	}
	
	public function fetchErsteller($id)
	{
 		$db = Zend_Db_Table::getDefaultAdapter();
 		$entry = new Zend_Db_Select($db);
		$entry->from('turnbericht');
		$entry->where('turnbericht.ersteller = ?', $id);
		return $entry->query()->fetchall();
	}
	
	public function fetchTurnandID($turn, $id)
	{
 		$db = Zend_Db_Table::getDefaultAdapter();
 		$entry = new Zend_Db_Select($db);
		$entry->from('turnbericht');
		$entry->where('turnbericht.turn = ?', $turn);
		$entry->where('turnbericht.id = ?', $id);
		return $entry->query()->fetch();
	}

	public function update(Application_Model_Turnbericht $turnbericht)
	{
		$now = new DateTime();
	
		$data = array(
					'inhalt' => $turnbericht->getInhalt(),
					'tag' => $turnbericht->getTag(),
					'distanz' => $turnbericht->getDistanz(),
					'starthafen' => $turnbericht->getStarthafen(),
					'zielhafen' => $turnbericht->getZielhafen()
		);
		$this->getDbTable()->update($data, array('id = ?' => $turnbericht->getId()));
	}
	
	public function setpapierkorb($id)
	{
		$data = array(
				'papierkorb' => true);
		return $this->getDbTable()->update($data, array(
				'id = ?' => $id));
	}
}




